package com.qi.demo01.Lock8;

import java.util.concurrent.TimeUnit;

/**
 * @author zjq
 * @date 2020/3/31 20:46
 */
//死锁
public class DeadLock {
    public static void main(String[] args) {
        String LockA = "lockA";
        String LockB = "lockB";
        new Thread(new lockTest(LockA, LockB)).start();
        new Thread(new lockTest(LockB, LockA)).start();

    }
}

class lockTest implements Runnable {
    private String LockA;
    private String LockB;

    public lockTest(String lockA, String lockB) {
        LockA = lockA;
        LockB = lockB;
    }

    @Override
    public void run() {
        synchronized (LockA) {
            System.out.println(Thread.currentThread().getName() + "lock:" + LockA + "+=get" + LockB);
            try {
                TimeUnit.SECONDS.sleep(3);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            synchronized (LockB) {
                System.out.println(Thread.currentThread().getName() + "lock:" + LockA + "+=get" + LockB);
            }
        }
    }
}
